package models

import (
	"example.com/myproject/project/global"
)
// 可直接在结构体中嵌入gorm.Model, 以便自动包含gorm字段
// 嵌入gorm.Model会多创建三个字段：创建时间，修改时间，
type User struct {
	ID       uint            `gorm:"primaryKey"`                  // 主键
	Username string          `gorm:"not null" gorm:"uniqueIndex"` // 姓名,确保用户名唯一
	Password string          `gorm:"not null"`                    // 密码
	UserType global.UserType `gorm:"not null"`                    // 用户类型
}

type OrderInfo struct {
	OrderId        uint   `json:"order_id"`
	UserId         uint   `json:"user_id"`
	FlightId       uint   `json:"flight_id"`
	DepartureCity  string `json:"departure_city"`
	ArrivalCity    string `json:"arrival_city"`
	DepartureTime  string `json:"departure_time"`
	FlightDuration string `json:"flight_duration"`
}

func GetOrderById(id uint, orders *[]OrderInfo) error {
	err := global.Db.Model(&Order{}).Select("orders.order_id, orders.user_id, orders.flight_id, flights.departure_city, "+
		"flights.arrival_city, flights.departure_time, flights.flight_duration").
		Joins("JOIN flights ON orders.flight_id = flights.flight_number").
		Where("orders.user_id = ?", id).
		Scan(orders).Error
	if err != nil {
		return err
	} else {
		return nil
	}
}
